package com.jxfgyj.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jxfgyj.dao.UserDao;
import com.jxfgyj.domain.DO.jxfgyj_user;

public class UserDaoImpl implements UserDao {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return this.sessionFactory.getCurrentSession();
	}

	/*
	 * 
	 */

	@Override
	public boolean createUser(jxfgyj_user user) {
		Session session = getSession();
		session.save(user);
		session.flush();
		return true;
	}

	@Override
	public List<jxfgyj_user> listUser() {
		Session session = getSession();
		String hql = "from jxfgyj_user ";
		//
		Query query = session.createQuery(hql);
		//

		//
		List<jxfgyj_user> userList = query.list();
		session.clear();
		//
		return userList;
	}

	@Override
	public void deleteUser_byID(String user_id) {
		Session session = getSession();
		String hql = "delete from jxfgyj_user where user_id=:user_id";
		Query query = session.createQuery(hql);
		//
		query.setParameter("user_id", user_id);
		//
		query.executeUpdate();
		session.flush();

	}
	/*
	 * 
	 */

	@Override
	public jxfgyj_user getUser_byLogin(jxfgyj_user user) {
		String hql = "from jxfgyj_user where user_account='" + user.getUser_account() + "' and user_password='"
				+ user.getUser_password() + "'";
		Query query = getSession().createQuery(hql);
		jxfgyj_user user2 = (jxfgyj_user) query.uniqueResult();
		return user2;

	}

}
